#include <gui/containers/MenuElement.hpp>

MenuElement::MenuElement():imageClickHandler(this,&MenuElement::image_click_handler)
{

}
//
void MenuElement::initialize()
{
    MenuElementBase::initialize();
}
//重新设置组件的x轴位置
void MenuElement::offset(int16_t x)
{
		//设置图标位置
		icon.moveTo(20 + x, icon.getY());
		//设置文本位置，文本跟随图标移动
		text.moveTo(43 + icon.getX(), 5 + icon.getY());	
}
//点击图标后触发事件
void MenuElement::image_click_handler(const touchgfx::ScalableImage &image , const touchgfx::ClickEvent &event)
{
		touchgfx::ClickEvent::ClickEventType type = event.getType();
		if(type==touchgfx::ClickEvent::ClickEventType::PRESSED)
		{
				if(&image == &icon)
				{
						switch(number % 7)
						{
								case 0:		//健康监测事件触发，健康监测图标与文字
									icon.setBitmap(touchgfx::Bitmap(BITMAP_APPLICATIONPAGE_IMAGE_PRESSED_HEALTH_ID));
									icon.invalidate();
									application().gotoHealthPageScreenCoverTransitionEast();
									break;
								case 1:		//姿态读取事件触发，姿态读取图标与文字
									icon.setBitmap(touchgfx::Bitmap(BITMAP_APPLICATIONPAGE_IMAGE_PRESSED_6050_ID));
									icon.invalidate();
									application().gotoSixAxisPageScreenCoverTransitionEast();
									break;
								case 2:		//事件触发，环境信息图标与文字
									icon.setBitmap(touchgfx::Bitmap(BITMAP_APPLICATIONPAGE_IMAGE_PRESSED_TEMP_ID));
									icon.invalidate();
									application().gotoInfoPageScreenCoverTransitionEast();
								break;
								case 3:		//芯片信息事件触发，芯片信息图标与文字
									icon.setBitmap(touchgfx::Bitmap(BITMAP_APPLICATIONPAGE_IMAGE_PRESSED_CHIP_ID));
									icon.invalidate();
									application().gotoChipPageScreenCoverTransitionEast();
									break;
								case 4:		//振动控制事件触发，振动控制图标与文字
									icon.setBitmap(touchgfx::Bitmap(BITMAP_APPLICATIONPAGE_IMAGE_PRESSED_BUZZER_ID));
									icon.invalidate();
									application().gotoMotorPageScreenCoverTransitionEast();
									break;
								case 5:		//电池电量事件触发，电池电量图标与文字
									icon.setBitmap(touchgfx::Bitmap(BITMAP_APPLICATIONPAGE_IMAGE_PRESSED_BATTERY_ID));
									icon.invalidate();
									application().gotoBatteryPageScreenCoverTransitionEast();
									break;
								case 6:		//无线连接事件触发，无线连接图标与文字
									icon.setBitmap(touchgfx::Bitmap(BITMAP_APPLICATIONPAGE_IMAGE_PRESSED_WIFI_ID));
									icon.invalidate();
									application().gotoWiFiPageScreenCoverTransitionEast();
									break;
						}
				}
		}
}
//
void MenuElement::setY(int16_t y)
{
		MenuElementBase::setY(y);
		//设定圆弧的半径，表盘直径-图标半径
		const int circleRadius = 210;
		//获取Y轴坐标信息	
		y = y + getHeight()/2 - 240 / 2;
		//勾股定理计算X轴位置
		float x_f = circleRadius - sqrtf(abs((float)(circleRadius * circleRadius) - (y * y)));
		//重新设置组件的x轴位置
		offset((int16_t)(x_f));
}
//对const的buf指针做强转去掉const
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wcast-qual"
//设置图标与文本
void MenuElement::setNumber(int no)
{
		icon.setClickAction(imageClickHandler);
		//修改图标
		switch(no % 7)
		{
				case 0:		//无触发事件时，健康监测图标与文字
					icon.setBitmap(touchgfx::Bitmap(BITMAP_APPLICATIONPAGE_IMAGE_HEALTH_ID));
					Unicode::snprintf(textBuffer, TEXT_SIZE, (Unicode::UnicodeChar*)L"健康监测");
					break;
				case 1:		//无触发事件时，姿态读取图标与文字
					icon.setBitmap(touchgfx::Bitmap(BITMAP_APPLICATIONPAGE_IMAGE_6050_ID));
					Unicode::snprintf(textBuffer, TEXT_SIZE, (Unicode::UnicodeChar*)L"姿态感知");
					break;
				case 2:		//无触发事件时，环境信息图标与文字
					icon.setBitmap(touchgfx::Bitmap(BITMAP_APPLICATIONPAGE_IMAGE_TEMP_ID));
					Unicode::snprintf(textBuffer, TEXT_SIZE, (Unicode::UnicodeChar*)L"环境信息");
					break;
				case 3:		//无触发事件时，芯片信息图标与文字
					icon.setBitmap(touchgfx::Bitmap(BITMAP_APPLICATIONPAGE_IMAGE_CHIPINFO_ID));
					Unicode::snprintf(textBuffer, TEXT_SIZE, (Unicode::UnicodeChar*)L"芯片信息");
					break;
				case 4:		//无触发事件时，振动控制图标与文字
					icon.setBitmap(touchgfx::Bitmap(BITMAP_APPLICATIONPAGE_IMAGE_BUZZER_ID));
					Unicode::snprintf(textBuffer, TEXT_SIZE, (Unicode::UnicodeChar*)L"外部控制");
					break;
				case 5:		//无触发事件时，电池电量图标与文字
					icon.setBitmap(touchgfx::Bitmap(BITMAP_APPLICATIONPAGE_IMAGE_BATTERY_ID));
					Unicode::snprintf(textBuffer, TEXT_SIZE, (Unicode::UnicodeChar*)L"电池电量");
					break;
				case 6:		//无触发事件时，无线连接图标与文字
					icon.setBitmap(touchgfx::Bitmap(BITMAP_APPLICATIONPAGE_IMAGE_WIFI_ID));
					Unicode::snprintf(textBuffer, TEXT_SIZE, (Unicode::UnicodeChar*)L"无线连接");
					break;
		}
		number = no;
}